import { _decorator, AnimationClip, Node, Animation } from 'cc'
import {
  ANIMATION_GRAPH_TYPE_ENUM,
  ENTITY_STATE_ENUM,
  ENTITY_TYPE_ENUM,
  SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM,
} from '../enums'
import { StateMachine } from '../base/StateMachine'
import SpikesOneSubStateMachine from './SpikesOneSubStateMachine'
import SpikesTwoSubStateMachine from './SpikesTwoSubStateMachine'
import SpikesThreeSubStateMachine from './SpikesThreeSubStateMachine'
import SpikesFourSubStateMachine from './SpikesFourSubStateMachine'
import { SPIKES_ANIMATION_PATH } from '../utils/constants'
import { SpikesMgr } from './SpikesMgr'
const { ccclass, property } = _decorator
/** 地刺状态机 */
@ccclass('SpikesStateMachine')
export class SpikesStateMachine extends StateMachine {
  async init() {
    this.animationComponent = this.addComponent(Animation)
    this.initParams()
    this.initStateMachines()
    this.initAnimationEvent()
    await Promise.all(this.watingState)
  }

  /** 初始化参数列表 */
  initParams() {
    this.params.set(ENTITY_STATE_ENUM.SPIKES_CUR_COUNT, { type: ANIMATION_GRAPH_TYPE_ENUM.NUMBER, value: 0 })
    this.params.set(ENTITY_STATE_ENUM.SPIKES_TOTAL_COUNT, { type: ANIMATION_GRAPH_TYPE_ENUM.NUMBER, value: 0 })
  }

  /** 初始化状态机 */
  initStateMachines() {
    this.stateMachines.set(ENTITY_TYPE_ENUM.SPIKES_ONE, new SpikesOneSubStateMachine(this))
    this.stateMachines.set(ENTITY_TYPE_ENUM.SPIKES_TWO, new SpikesTwoSubStateMachine(this))
    this.stateMachines.set(ENTITY_TYPE_ENUM.SPIKES_THREE, new SpikesThreeSubStateMachine(this))
    this.stateMachines.set(ENTITY_TYPE_ENUM.SPIKES_FOUR, new SpikesFourSubStateMachine(this))
  }

  /** 动画播放完毕的处理 比如恢复成其他动画 */
  initAnimationEvent() {
    this.animationComponent.on(Animation.EventType.FINISHED, () => {
      const aniName = this.animationComponent.defaultClip.name
      const value = this.getParams(ENTITY_STATE_ENUM.SPIKES_TOTAL_COUNT)
      console.log(aniName, SPIKES_ANIMATION_PATH.spikesfourFive)

      if (
        (aniName === SPIKES_ANIMATION_PATH.spikesoneTwo && value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.spikes_one) ||
        (aniName === SPIKES_ANIMATION_PATH.spikestwoThree && value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.spikes_two) ||
        (aniName === SPIKES_ANIMATION_PATH.spikesthreeFour &&
          value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.spikes_three) ||
        (aniName === SPIKES_ANIMATION_PATH.spikesfourFive && value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM.spikes_four)
      ) {
        console.log(1)

        this.node.getComponent(SpikesMgr).resetSpikes()
      }
    })
  }

  /** 运行状态机 切换当前状态 */
  run() {
    const value = this.getParams(ENTITY_STATE_ENUM.SPIKES_TOTAL_COUNT) // 获取总点数
    switch (this.currentState) {
      case this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_ONE):
        if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM[ENTITY_TYPE_ENUM.SPIKES_ONE]) {
          // 总点数等于对应的映射值，说明是单刺的地刺
          this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_ONE)
        } else if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM[ENTITY_TYPE_ENUM.SPIKES_TWO]) {
          // 总点数等于对应的映射值，说明是双刺的地刺
          this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_TWO)
        } else if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM[ENTITY_TYPE_ENUM.SPIKES_THREE]) {
          // 总点数等于对应的映射值，说明是三刺的地刺
          this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_THREE)
        } else if (value === SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM[ENTITY_TYPE_ENUM.SPIKES_FOUR]) {
          // 总点数等于对应的映射值，说明是四刺的地刺
          this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_FOUR)
        } else {
          this.currentState = this.currentState // 触发StateMachine基类的set currentState方法
        }
        break
      default:
        this.currentState = this.stateMachines.get(ENTITY_TYPE_ENUM.SPIKES_ONE) // 默认单刺
        break
    }
  }
}
